<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of NewsArticleType
 *
 * @author bien.tran
 */
namespace Blog\BlogBundle\Form;

use Blog\BlogBundle\Entity\NewsCategories;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Blog\BlogBundle\Form\DataTransformer\CategoryToNameTransformer;
use Blog\BlogBundle\Form\EventListener\AddNameFieldSubscriber;
use Blog\BlogBundle\Form\TagType;

class NewsArticleType extends AbstractType {

    public function buildForm(FormBuilder $formBuildeer, array $options) {
        $transformer = new CategoryToNameTransformer();
        $formBuildeer->prependClientTransformer($transformer);
        
        $subscriber = new AddNameFieldSubscriber($formBuildeer->getFormFactory());
        $formBuildeer->addEventSubscriber($subscriber); // if article is updating, admin don't want user update title, field title is not show
        //$formBuildeer->add('title', 'text', array('label' => 'Title of article', 'required' => true));
        $formBuildeer->add('content', 'textarea', array('required' => false));
        
        
        
        $formBuildeer->add('tags', 'collection', array('type' => new TagType(), 'allow_add' => true, 'by_reference' => false));
        
        
        $formBuildeer->add('category', 'entity', array(
            'class' => 'BlogBundle:NewsCategories',
            'label' => 'Section for category',
            'query_builder' => function (\Doctrine\ORM\EntityRepository $pdb) {
                return $pdb->createQueryBuilder('cat')->orderBy('cat.name');
            },
                    'property' => 'name',
                    'empty_value' => '------- please choose -------',
            'required' => true)); // type here can using NewsCategoryType
        
        $formBuildeer->add('image', 'file', array('label' => 'Image'));
        $formBuildeer->add('active', 'checkbox', array('label' => 'Publish article'));
        
    }
    public function getDefaultOptions(array $options) {
        return array('data_class' => 'Blog\BlogBundle\Entity\NewsArticles'); //'virtual' => true to use common fields from another table
    }
    public function getName() {
        return 'blog_blogbundle_newsarticletype';
    }
}

?>
